
export class UploadHelper {
  private readonly TAG: string = 'MediaHelper';

  // private uContext: common.Context;
  //
  // constructor(context: common.Context) {
  //   this.uContext = context;
  // }

  /**
   * 构建post 请求参数
   * @returns
   */
  public buildTaskItemFormData(dto: CheckTaskPhotoDTO, file: FileConfig): FormData {
    let formData = new FormData()
    let boundary = Math.random().toString(36).slice(-8);
    formData.contentType = "multipart/form-data; boundary=" + boundary
    formData.body = `--${boundary}\r\n` +
    `Content-Disposition: form-data; name="taskId"\r\nContent-Type: application/json\r\n\r\n${dto.taskId}\r\n--${boundary}\r\n` +
    `Content-Disposition: form-data; name="itemId"\r\nContent-Type: application/json\r\n\r\n${dto.itemId}\r\n--${boundary}\r\n` +
    `Content-Disposition: form-data; name="file"; filename="${file.filename}"\r\nContent-Type: jpg\r\n\r\n${file.content}\r\n--${boundary}--\r\n`
    // formData.body = "--" + splitStr + "\r\n" +
    // "Content-Disposition: form-data; name=\"taskId\"" + "\r\n" +
    // "Content-Length: " + content.length + "\r\n\r\n" +
    // content + "\r\n" +
    // "--" + splitStr + "--"
    return formData
  }
}

let uploadHelper = new UploadHelper();

export default uploadHelper as UploadHelper;

export class FormData {
  body: string;
  contentType: string;

  constructor(body: string = '', contentType: string = '') {
    this.body = body;
    this.contentType = contentType;
  }
}

export class CheckTaskPhotoDTO {
  taskId: number;
  itemId: number;

  constructor(taskId: number, itemId: number) {
    this.taskId = taskId;
    this.itemId = itemId;
  }
}

export class FileConfig {
  filename: string;
  name: string;
  uri: string;
  type: string;
  content: number;

  constructor(filename: string, name: string, uri: string, type: string, content: number) {
    this.filename = filename;
    this.name = name;
    this.uri = uri;
    this.type = type;
    this.content = content;
  }
}

